package com.race.car;

import java.util.Vector;

import org.andengine.entity.scene.Scene;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.extension.physics.box2d.util.constants.PhysicsConstants;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import android.util.Log;

import com.badlogic.gdx.math.Vector2;
import com.race.constants.RaceConstants;
import com.race.map.RaceMap;

public class NPCCar extends Car {

	private RaceMap mMap;
	public NPCCar(float pX, float pY,
			ITiledTextureRegion pTiledTextureRegion,
			VertexBufferObjectManager pVertexBufferObjectManager,
			PhysicsWorld pPhysicsWorld, Scene pScene) {
		super(pX, pY, pTiledTextureRegion, pVertexBufferObjectManager, pPhysicsWorld,
				pScene);
	}

	public NPCCar(float pX, float pY, float pWidth, float pHeight,
			VertexBufferObjectManager pVertexBufferObjectManager,
			PhysicsWorld pPhysicsWorld, Scene pScene) {
		super(pX, pY, pWidth, pHeight, pVertexBufferObjectManager, pPhysicsWorld,
				pScene);
	}
	public void registerMap(RaceMap pMap){
		mMap=pMap;
	}
	@Override
	public void onUpdate(float pSecondsElapsed){
		if(mMap!=null&&pSecondsElapsed>0.00001f){
			byte roadDirection=mMap.getRoadDirection(this);
			float pi=3.14159265359f;
			Vector2 direction = null;
			if(roadDirection==RaceConstants.ROAD_DIRECTION_UP)
				direction=RaceConstants.VECTOR_UP;
			else if(roadDirection==RaceConstants.ROAD_DIRECTION_DOWN)
				direction=RaceConstants.VECTOR_DOWN;
			else if(roadDirection==RaceConstants.ROAD_DIRECTION_LEFT)
				direction=RaceConstants.VECTOR_LEFT;
			else if(roadDirection==RaceConstants.ROAD_DIRECTION_RIGHT)
				direction=RaceConstants.VECTOR_RIGHT;
//			Log.i("direction",direction.toString());
			float pos[]=getHeadCoordinates(),rx,ry;
			rx=(float)(Math.random()-0.5)/10;ry=(float)(Math.random()-0.5)/10;
			if(direction!=null)
				getBody().applyForce(direction.x*2f+rx, direction.y*2f+ry, pos[0]/PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT, pos[1]/PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT);
		}
	}
}
